package com.slipper.system.subsystem.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.common.service.CommonService;
import com.slipper.constants.Constants;
import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.subsystem.dao.SubsystemDao;
import com.slipper.system.subsystem.bo.SubsystemBO;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class SubsystemService {
    @Resource
    private SubsystemDao subsystemDao;
    @Resource
    private CommonService commonService;

    public SubsystemBO queryBy(String id) {
        SubsystemBO subsystem = CacheUtil.get(CacheKey.Subsystem.PREFIX + id, SubsystemBO.class);
        if (subsystem == null) {
            subsystem = subsystemDao.queryBy(id);
            CacheUtil.set(CacheKey.Subsystem.PREFIX + id, subsystem, 60 * 60);
        }
        return subsystem;
    }

    public List<SubsystemBO> queryList(SubsystemBO bo) {
        return subsystemDao.queryList(bo);
    }

    public List<SubsystemBO> queryList(String[] ids) {
        return subsystemDao.queryListByIds(ids);
    }

    public PageInfo<SubsystemBO> queryPage(SubsystemBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(subsystemDao.queryList(bo));
    }

    @WriteTransactional
    public int save(SubsystemBO bo) {
        if (Tool.isBlank(bo.getType())) {
            bo.setType(Constants.Subsystem.TYPE_SUBSYSTEM);
        }

        int result;
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            result = subsystemDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result = subsystemDao.insert(bo);
            createFolder(bo.getFolderName(), bo.getOperateId());
        }
        CacheUtil.remove(CacheKey.Subsystem.PREFIX + bo.getId());
        return result;
    }

    @WriteTransactional
    public void createFolder(String folderName, String operateId) {
        if (Tool.isBlank(folderName)) return;
        createFolder("cl_data_folder", folderName, operateId);
        createFolder("cl_interface_folder", folderName, operateId);
        createFolder("cl_page_folder", folderName, operateId);
        createFolder("cl_script_folder", folderName, operateId);
        createFolder("cl_template_folder", folderName, operateId);
        createFolder("cl_variable_folder", folderName, operateId);
        createFolder("cl_view_folder", folderName, operateId);
        createFolder("sys_dictionary", folderName, null);
    }

    @WriteTransactional
    public void createFolder(String tableName, String folderName, String operateId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", Tool.getUUID());
        paramMap.put("parent_id", "0");
        paramMap.put("name", folderName);
        if (Tool.isNotBlank(operateId)) {
            paramMap.put("update_id", operateId);
            paramMap.put("create_id", operateId);
        }
        paramMap.put("update_time", System.currentTimeMillis());
        paramMap.put("create_time", System.currentTimeMillis());
        commonService.insert(tableName, paramMap);
    }

    @WriteTransactional
    public void saveList(List<SubsystemBO> subsystemList) {
        if (subsystemList == null) return;

        subsystemList.forEach(subsystem -> {
            if (subsystemDao.queryBy(subsystem.getId()) != null) {
                subsystem.setUpdateTime(System.currentTimeMillis());
                subsystemDao.update(subsystem);
            } else {
                subsystem.setUpdateTime(System.currentTimeMillis());
                subsystem.setCreateTime(System.currentTimeMillis());
                subsystemDao.insert(subsystem);
            }
            CacheUtil.remove(CacheKey.Subsystem.PREFIX + subsystem.getId());
        });
    }

    @WriteTransactional
    public int delete(SubsystemBO bo) {
        int result = subsystemDao.delete(bo);
        CacheUtil.remove(CacheKey.Subsystem.PREFIX + bo.getId());
        return result;
    }
}
